package com.usian.controller;

import com.usian.Result;
import com.usian.feign.SsoFeign;
import com.usian.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/frontend/sso")
@RestController
public class SsoController {

    @Autowired
    private SsoFeign ssoFeign;

    /**
     * 校验用户注册信息
     *
     * @param checkValue
     * @param checkFlag
     * @return
     */
    @RequestMapping("checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue,
                                @PathVariable int checkFlag){
        boolean result = ssoFeign.checkUserInfo(checkValue,checkFlag);
        //true 有 false 没有
        if(!result){
            return Result.ok(result);
        }
        return Result.error("true:用户存在");
    }

    /**
     * 用户注册
     *
     * @param tbUser
     * @return
     */
    @RequestMapping("userRegister")
    public Result userRegister(TbUser tbUser){

        boolean result = ssoFeign.userRegister(tbUser);

        if(result){
            return Result.ok();
        }
        return Result.error("注册失败");
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("userLogin")
    public Result userLogin(String username,String password){
        Map map =ssoFeign.userLogin(username,password);
        if(map!=null){
            return Result.ok(map);
        }
        return Result.error("登录失败");
    }

    /**
     * 更新redis中token的有效期
     *
     * @param token
     * @return
     */
    @RequestMapping("getUserByToken/{token}")
    public Result getUserByToken(@PathVariable String token){
        boolean bool = ssoFeign.getUserByToken(token);
        if(bool){
            return Result.ok();
        }
        return Result.error("跟新失败");
    }

}
